import java.nio.charset.Charset;

public class StringUtil
{
    public static byte[] dataToByteArray(String s)
    {
        byte[] btAryData = new byte[s.length()];
        for (int i = 0; i < btAryData.length; i++)
        {
            btAryData[i] = (byte)s.charAt(i);
        }
        return btAryData;
    }

    public static String toHexString(String src)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < src.length(); i++)
        {
            char c = src.charAt(i);
            sb.append(Integer.toHexString(c));
        }
        return sb.toString();
    }

    public static String toCharStr(String hexStr)
    {
        if(hexStr.length() % 2 == 0)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hexStr.length(); i += 2)
            {
                String s = hexStr.substring(i , i+2);
                int v = Integer.valueOf(s, 16);
                sb.append((char)v);
            }
            return sb.toString();
        }
        return "";
    }

    public static String byteArrayToString(byte[] ar)
    {
        return new String(ar, Charset.forName("US-ASCII"));
    }

    public static int dataLength(String s)
    {
        return s.length();
    }

}